<?php

namespace App\Exports;

use App\Models\Task;
use App\Models\Depart;
use App\Models\Service;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class TaskExport implements FromView
{
   	public function __construct($where)
    {
        $this->where = $where;
    }

    public function view(): View
    {
    	$list = Task::where($this->where)->get();
    	foreach ($list as $key => $v) {
    		if($v->service) {
    			$service_names   = $v->service->pluck('service_name')->toArray();
    			
    			$list[$key]['service_name'] = implode(',', $service_names);		
    		}

    		if($v->depart) {
    			$names   = $v->depart->pluck('depart_name')->toArray();
    			
    			$list[$key]['depart_name'] = implode(',', $names);		
    		}
    						
    	}
        return view('admin.task.export_index', compact('list'));
    }
}
